﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using ChapterX.Common;
using ChapterX.Data;
using ChapterX.Data.CoursesDSTableAdapters;
using ChapterX.Data.BooksDSTableAdapters;
using ChapterX.Data.DigitizationDSTableAdapters;
using System.Data.SqlClient;

namespace ChapterX.BusinessFacade
{
    public class ApplyDigitization
    {
        public bool ApplyForDigitization(int courseId, BooksDS.BooksRow book, CoursesDS.CourseBookRow coursebook,
            DigitizationDS.DigitizationRow digitization, DigitizationDS.DigitizationLogsRow digitizationlog, DateTime submitTime)
        {
            //TODO 判断book 和course 是否存在！！！！

            CourseSystem cs = new CourseSystem();
            BookSystem bs = new BookSystem();
            DigitizationSystem ds = new DigitizationSystem();

            //CoursesTableAdapter courseAdapter = new CoursesTableAdapter();
            BooksTableAdapter bookAdapter = new BooksTableAdapter();
            CourseBookTableAdapter coursebookAdapter = new CourseBookTableAdapter();
            DigitizationTableAdapter digitizationAdapter = new DigitizationTableAdapter();
            DigitizationLogsTableAdapter digitizationLogAdapter = new DigitizationLogsTableAdapter();

            SqlConnection conn = bookAdapter.Connection;
            //bookAdapter.Connection = conn;
            coursebookAdapter.Connection = conn;
            digitizationAdapter.Connection = conn;
            digitizationLogAdapter.Connection = conn;

            conn.Open();

            SqlTransaction trans = conn.BeginTransaction();
            //courseAdapter.Transaction = trans;
            coursebookAdapter.Transaction = trans;
            bookAdapter.Transaction = trans;
            digitizationAdapter.Transaction = trans;
            digitizationLogAdapter.Transaction = trans;

            try
            {
                
                bool result2 = false;

                BooksDS.BooksDataTable bookData = bookAdapter.GetBookByBookId(book.BookId);
                if (bookData.Rows.Count == 0)
                {
                    //result2 = bs.InsertBook(bookAdapter, book, submitTime);
                }

                if (result2)
                {
                    coursebook.CourseId = courseId;
                    coursebook.BookId = book.BookId;
                    coursebook.Status = 0;

                    bool result3 = false; 
                    //bool result3 = cs.InsertCourseBook(coursebookAdapter, coursebook, submitTime);

                    digitization.BookId = book.BookId;
                    bool result4 = ds.InsertDigitization(digitizationAdapter, digitization, submitTime);

                    digitizationlog.BookId = book.BookId;
                    bool result5 = ds.InsertDigitizationLog(digitizationLogAdapter, digitizationlog, submitTime);

                    if (result3 && result4 && result5)
                    {
                        trans.Commit();
                        return true;
                    }
                }

                trans.Rollback();
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }

        public bool SubmitCourse(CoursesDS.CoursesRow course)
        {
            CourseSystem cs = new CourseSystem();
            
            CoursesTableAdapter courseAdapter = new CoursesTableAdapter();
            TeacherCourseTableAdapter teachercourseAdapter = new TeacherCourseTableAdapter();

            SqlConnection conn = courseAdapter.Connection;
            teachercourseAdapter.Connection = conn;

            conn.Open();

            SqlTransaction trans = conn.BeginTransaction();
            courseAdapter.Transaction = trans;
            teachercourseAdapter.Transaction = trans;
            
            try
            {

                bool result1 = false;
                bool result2 = false;

                CoursesDS.CoursesDataTable data = courseAdapter.GetCourseByCourseId(course.CourseId);


                int? courseId = null;
                if (data.Rows.Count == 0)
                {
                    // 课程信息不存在，插入课程信息
                    courseAdapter.InsertCourse(ref courseId, course.CourseNumber,
                        course.CourseName,
                        course.Department,
                        course.Semester,
                        course.Audience,
                        course.Credit,
                        course.Assessment,
                        course.Volume,
                        course.TeacherId,
                        course.SubmitUser);
                    
                    /*
                    courseAdapter.InsertCourse(ref courseId, 
                    course.CourseName, 
                    course.CourseNumber, 
                    //course.IsCollegeNull() ? null : course.College.Trim(),
                    course.IsDepartmentNull() ? null : course.Department.Trim(), 
                    course.IsAudienceNull() ? null : course.Audience.Trim(), 
                    course.IsCreditNull() ? (int?)null : course.Credit, 
                    //course.IsTotalHoursNull() ? (int?)null : course.TotalHours,
                    course.TeacherId,
                    //course.TeacherName,
                    //course.IsYearNull() ? null : course.Year, 
                    course.IsSemesterNull() ? null : course.Semester, 
                    //course.IsDescriptionNull() ? null : course.Description, 
                    //course.IsStuNumberNull() ? (int?)null : course.StuNumber,
                    course.IsExamineTypeNull()? null : course.ExamineType,
                    DateTime.Now);
                    */
                    if (courseId.HasValue)
                    {
                        course.CourseId = courseId.Value;
                        result1 = true;
                    }

                    int r;
                    if (result1)
                    {
                        r = teachercourseAdapter.InsertTeacherCourse(
                            course.TeacherId,
                            course.CourseId);

                        if (r > 0)
                        {
                            result2 = true;
                        }
                    }

                    if (result1 && result2)
                    {
                        trans.Commit();
                        return true;
                    }
                }
                trans.Rollback();
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }

        //public bool DigitizingApplication(int bookId)
        //{
        //    DigitizationSystem ds = new DigitizationSystem();

        //    DigitizationTableAdapter digitizationAdapter = new DigitizationTableAdapter();
        //    DigitizationLogsTableAdapter digitizationLogAdapter = new DigitizationLogsTableAdapter();

        //    SqlConnection conn = digitizationAdapter.Connection;
        //    digitizationAdapter.Connection = conn;
        //    digitizationLogAdapter.Connection = conn;

        //    conn.Open();

        //    SqlTransaction trans = conn.BeginTransaction();
        //    digitizationAdapter.Transaction = trans;
        //    digitizationLogAdapter.Transaction = trans;

        //    bool result1 = false;
        //    bool result2 = false;

        //    result1 = ds.UpdateDigitizationStatus(bookId, (int)CommonDefinition.DigitizationStatus.digitizing);

        //    DigitizationDS.DigitizationLogsRow digitizationlog = ds.DigitizationLogSchema.NewDigitizationLogsRow();
        //    digitizationlog.BookId = bookId;
        //    digitizationlog.DigitizationStatus = (int)CommonDefinition.DigitizationStatus.digitizing;

        //    UsersDS.UsersRow user = CurrentUserInfo.User;
        //    digitizationlog.OperatorId = user.Username;
        //    digitizationlog.OperatorName = user.RealName;
        //    digitizationlog.Operation = CommonDefinition.DigitizationOperation_Digitizing;

        //    result2 = ds.InsertDigitizationLog(digitizationLogAdapter, digitizationlog, DateTime.Now);
            
        //    if (result1 && result2)
        //    {    
        //        trans.Commit();
        //        return true;
        //    }

        //    trans.Rollback();
        //    return false;
        //}
    }
}
